﻿namespace GuaHao
{
    public partial class AddPatientForm : Form
    {
        string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data", "data_patient.txt");
        //患者表
        private List<Patients> patients = new List<Patients>();
        public Patients Patient { get; private set; }
        bool isEditMode;
        // 构造函数（用于新增患者）
        public AddPatientForm()
        {
            InitializeComponent();
            isEditMode = false; // 新增模式
        }
        // 构造函数（用于编辑患者）
        public AddPatientForm(Patients patient)
        {
            InitializeComponent();
            Patient = patient;
            LoadPatientData(patient);
            isEditMode = true; // 编辑模式
            this.Text = "患者信息修改";
        }
        // 加载患者信息
        private void LoadPatientData(Patients patient)
        {
            TboxPatientName.Text = patient.Name;
            RadioButtonMan.Checked = patient.Gender == "男";
            RadioButtonWoman.Checked = patient.Gender == "女";
            NupAge.Value = patient.Age;
            TboxIdcard.Text = patient.IDCard;
            TboxPhone.Text = patient.Phone;
        }
        // 保存患者信息
        private void AddBtn_Click(object sender, EventArgs e)
        {
            if (ValidateInput())
            {
                patients = TextJson.ReadPatients(path);
                if (!isEditMode)
                {
                    // 新增模式
                    int maxID = patients.Count > 0 ? patients.Max(u => u.PatientID) + 1 : 1; // 自增 ID
                    Patient = new Patients
                    {
                        PatientID = maxID,
                        Name = TboxPatientName.Text,
                        Gender = RadioButtonMan.Checked ? "男" : "女",
                        Age = (int)NupAge.Value,
                        IDCard = TboxIdcard.Text,
                        Phone = TboxPhone.Text,
                        CreateTime = DateTime.Now,
                        Modifytime = DateTime.Now
                    };

                    // 保存患者信息到 JSON 文件
                    patients.Add(Patient);
                }
                else
                {
                    // 编辑模式：直接修改传入的 Patient 对象
                    if (Patient != null) // 确保 Patient 对象不为空
                    {
                        Patient.Name = TboxPatientName.Text;
                        Patient.Gender = RadioButtonMan.Checked ? "男" : "女";
                        Patient.Age = (int)NupAge.Value;
                        Patient.IDCard = TboxIdcard.Text;
                        Patient.Phone = TboxPhone.Text;
                        Patient.Modifytime = DateTime.Now; // 更新修改时间

                        // 更新患者信息
                        var existingPatient = patients.FirstOrDefault(p => p.PatientID == Patient.PatientID);
                        if (existingPatient != null)
                        {
                            existingPatient.Name = Patient.Name;
                            existingPatient.Gender = Patient.Gender;
                            existingPatient.Age = Patient.Age;
                            existingPatient.IDCard = Patient.IDCard;
                            existingPatient.Phone = Patient.Phone;
                            existingPatient.Modifytime = Patient.Modifytime;
                        }
                    }
                    else
                    {
                        MessageBox.Show("患者信息加载失败，请重试！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }

                // 保存到 JSON 文件
                TextJson.WritePatients(patients, path);

                DialogResult = DialogResult.OK;
                Close();
            }
            else
            {
                MessageBox.Show("请填写完整信息！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        // 校验输入
        private bool ValidateInput()
        {
            return !string.IsNullOrEmpty(TboxPatientName.Text) &&
                   !string.IsNullOrEmpty(TboxIdcard.Text) &&
                   !string.IsNullOrEmpty(TboxIdcard.Text);
        }
        //取消
        private void ClassBtn_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}
